<?php
/**
 * Author: xx.com
 * Date: 2016-03-19
 */

namespace Home\Logic;

use Think\Model\RelationModel;
/**
 *
 * Class orderLogic
 * @package Home\Logic
 */
class OrderLogic extends RelationModel
{
    //
    public function check_order_outtime(){

        //获取十五分钟前时间戳
        $mark_time = time() -  900;
        //查询超过十五分钟未支付订单，并修改状态；
        $where['add_time'] = array('lt',$mark_time);
        $where['pay_status'] = 0;
        $where['order_status'] = array('neq', 5);
        $order_list = M('order')->field('order_id,order_sn,is_groupbuy,order_status,pay_status,add_time')->where($where)->select();
        $action_note = '逾期未支付，订单作废';
        $status_desc = '订单作废';
        $data['order_status'] = 5;
        $data['admin_note'] = $action_note;

        $result = [];

        foreach($order_list as $key => $value)
        {
            $close = M('order')->where([
                'order_sn' => $value['order_sn'],
                'pay_status' => 0,
            ])->save($data);

            if( $close )
            {
                logOrder($value['order_id'],$action_note,$status_desc,0);

                $closeGo = 0;

                if( $value['is_groupbuy'] == 1 )
                {
                    $closeGo = \Common\Logic\GroupbuyLogic::closeGroupBuyOrderSn($value['order_sn']);
                }
				// 订单数据回归
				cancelOrderRegression($value['order_sn']);
                $result[] = [
                    "order_sn" => $value['order_sn'],
                    "close" => 1,
                    "is_groupbuy" => $value['is_groupbuy'],
                    "closeGo" => $closeGo
                ];
            }else{
                $result[] = [
                    "order_sn" => $value['order_sn'],
                    "close" => 0,
                    "is_groupbuy" => $value['is_groupbuy'],
                    "closeGo" => 0
                ];
            }
        }

        doLog(json_encode($result),"closeOrder");

        return $result;
    }
}